package com.caine.multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: CaineZhu
 * @Description: 同一个实例中synchronized修饰的方法互斥(包括)
 * @Date: Created in 15:05 2020/12/10
 * @Modified By:
 */
public class Demo05 {

    public static void main(String[] args) {
        ExecutorService executor = new ThreadPoolExecutor(1, 2,
                20L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2), (r, executors) -> {
            System.out.println("被拒绝的任务:" + r.toString());
        });
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executor.execute(new MyTask(i));
        }
    }

    public static class MyTask implements Runnable {
        private Integer name;

        public MyTask(Integer name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "MyTask{" +
                    "name=" + name +
                    '}';
        }

        @Override
        public void run() {
            System.out.println(name + ">>>" + Thread.currentThread());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
